package youleche.com.util;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import youleche.com.business.BRentVehicleInfo;
import youleche.com.business.BShare;
import youleche.com.business.BVehicleImageInfo;
import youleche.com.factory.MLLogFactory;
import youleche.com.model.MRentVehicleInfo;
import youleche.com.model.MShare;
import youleche.com.model.MVehicleImageInfo;
import youleche.com.model.MZzqy;
import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * 静态化工具类
 * @author ule
 *
 */
public class StaticUtil {
	/**
	 * 模板配置文件
	 */
	 private static Template template = null;
	 /**
	  * 静态化攻略分享文章
	  * @param share 分享攻略文章
	  * @param cfg freemarker配置
	  * @param request request对象
	  * @param response response对象
	  */
	 public static void staticShare(MShare share,Configuration cfg, HttpServletRequest request, HttpServletResponse response){
		 staticShareDetail(share, cfg, request, response);
		 staticShareList(cfg, request, response);
	 }
	 
	 /**
	  * 静态化组织骑游文章
	  * @param share 组织骑游
	  * @param cfg freemarker配置
	  * @param request request对象
	  * @param response response对象
	  */
	 
	 public static void staticZzqy(MZzqy zzqy,Configuration cfg, HttpServletRequest request, HttpServletResponse response){
		 staticZzqyDetail(zzqy, cfg, request, response);
	 }
	 /**
	  * 静态化攻略分享文章
	  * @param share 分享攻略文章
	  * @param cfg freemarker配置
	  * @param request request对象
	  * @param response response对象
	  */
	 @SuppressWarnings({ "unchecked", "rawtypes" })
	private static void staticShareDetail(MShare share,Configuration cfg, HttpServletRequest request, HttpServletResponse response){
		 try {
				cfg.setDefaultEncoding("UTF-8");
				template = cfg.getTemplate("qy/ftl/share.ftl");
				Map map = new HashMap();
		        map.put("share", share);
		        String path = request.getSession().getServletContext().getRealPath("") + "/qy/html/"+ share.getId()+".html";
				File file = new File(path);
		        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
		        template.process(map, out);
	            
	            out.flush();
	            out.close();
			} catch (Exception e) {
				System.out.println("静态化攻略出错");
				e.printStackTrace();
				MLLogFactory.exceptionLog("静态化攻略出错", "静态化攻略出错", e.getMessage(), request.getRemoteAddr());
			}
	 }
	 
	 /**
	  * 静态化组织骑游文章
	  * @param zzqy 组织骑游文章
	  * @param cfg freemarker配置
	  * @param request request对象
	  * @param response response对象
	  */
	 @SuppressWarnings({ "unchecked", "rawtypes" })
	private static void staticZzqyDetail(MZzqy zzqy,Configuration cfg, HttpServletRequest request, HttpServletResponse response){
		 try {
				cfg.setDefaultEncoding("UTF-8");
				template = cfg.getTemplate("qy/ftl/zzqy.ftl");
				Map map = new HashMap();
		        map.put("zzqy", zzqy);
		        String path = request.getSession().getServletContext().getRealPath("") + "/qy/html/"+ zzqy.getId()+".html";
				File file = new File(path);
		        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
		        template.process(map, out);
	            
	            out.flush();
	            out.close();
			} catch (Exception e) {
				System.out.println("静态化攻略出错");
				e.printStackTrace();
				MLLogFactory.exceptionLog("静态化攻略出错", "静态化攻略出错", e.getMessage(), request.getRemoteAddr());
			}
	 }
	 
	 /**
	  * 静态化分享list
	  * @param cfg  freemarker配置
	  * @param request request对象
	  * @param response response对象
	  */
	 @SuppressWarnings({ "rawtypes", "unchecked" })
	public static void staticShareList(Configuration cfg, HttpServletRequest request, HttpServletResponse response){
		 try {
				cfg.setDefaultEncoding("UTF-8");
				template = cfg.getTemplate("qy/ftl/one2.ftl");
				BShare handler = new BShare();
				List<MShare> list = handler.getShareList("0", "9", " order by addTime desc ");
				List<MShare> tempList = new LinkedList<MShare>();
				for (MShare mShare : list) {
					if(mShare.getTitle().length() > 30){
						mShare.setTitle(mShare.getTitle().substring(0, 30) + "...");
					}
					
					tempList.add(mShare);
				}
				
				Map map = new HashMap();
		        map.put("list", tempList);
		        String path = request.getSession().getServletContext().getRealPath("") + "/qy/html/one2.jsp";
				File file = new File(path);
		        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
		        template.process(map, out);
	            
	            out.flush();
	            out.close();
			} catch (Exception e) {
				System.out.println("静态化攻略列表出错");
				e.printStackTrace();
				MLLogFactory.exceptionLog("静态化攻略列表出错", "静态化攻略列表出错", e.getMessage(), request.getRemoteAddr());
			}
	 }
	 
	 /**
	  * 静态化推荐租车信息
	  * @param cfg  freemarker配置
	  * @param request request对象
	  * @param response response对象
	  */
	 @SuppressWarnings({ "rawtypes", "unchecked" })
	public static void staticRecommendRentInfo(Configuration cfg, HttpServletRequest request, HttpServletResponse response){
		 try {
				cfg.setDefaultEncoding("UTF-8");
				template = cfg.getTemplate("qy/ftl/recommend.ftl");
				BRentVehicleInfo handler = new BRentVehicleInfo();
				MRentVehicleInfo rentInfo = handler.getRecommendRentVehicleInfo();
				
				Map map = new HashMap();
		        map.put("rentInfo", rentInfo);
		        String path = request.getSession().getServletContext().getRealPath("") + "/qy/html/recommend.jsp";
				File file = new File(path);
		        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
		        template.process(map, out);
	            
	            out.flush();
	            out.close();
			} catch (Exception e) {
				System.out.println("静态化推荐租车信息出错");
				e.printStackTrace();
				MLLogFactory.exceptionLog("静态化推荐租车信息出错", "静态化推荐租车信息出错", e.getMessage(), request.getRemoteAddr());
			}
	 }
	 
	 /**
	  * 静态化骑游攻略首页轮播图片
	  * @param cfg  freemarker配置
	  * @param request request对象
	  * @param response response对象
	  */
	 @SuppressWarnings({ "rawtypes", "unchecked" })
	public static void staticQyPlaying(Configuration cfg, HttpServletRequest request, HttpServletResponse response){
		 try {
				cfg.setDefaultEncoding("UTF-8");
				template = cfg.getTemplate("qy/ftl/playing.ftl");
				BVehicleImageInfo imageHan = new BVehicleImageInfo();
				List<MVehicleImageInfo> imgList = imageHan.getImageByType_DISTINCT("5", 5);
					
				
				Map map = new HashMap();
		        map.put("imgList", imgList);
		        String path = request.getSession().getServletContext().getRealPath("") + "/qy/html/playing.jsp";
				File file = new File(path);
		        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
		        template.process(map, out);
	            
	            out.flush();
	            out.close();
			} catch (Exception e) {
				System.out.println("静态化骑游攻略首页轮播图片");
				e.printStackTrace();
				MLLogFactory.exceptionLog("静态化骑游攻略首页轮播图片出错", "静态化骑游攻略首页轮播图片出错", e.getMessage(), request.getRemoteAddr());
			}
	 }
}
